home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr10 / bagtag2.zip / BAG_UTIL.CB next >
Text File  |  1993-03-20  |  8KB  |  298 lines

  1. /****************************************************************************
  2. *
  3. *   bag_util.cb                                         20Mar93
  4. *
  5. *   Misc. routines: utility functions
  6. *
  7. *   Contains macros:
  8. *       read_marked
  9. *       getyorn
  10. *       _chk_abort
  11. *       GetTempDir
  12. *       GetIdentifier
  13. *       _emPrompt1 (from EditMaster)
  14. *
  15. *   Copyright (c) 1990-1993 B. Goldstein -- Pequod Software
  16. *
  17. *   Change History (see also individual entries):
  18. *   Date    Who What
  19. *   ------- --- ---------------------------
  20. *   19Apr90 BAG First draft
  21. *   04May92 BAG Added _chk_abort
  22. *   04Jul92 BAG Added GetTempDir
  23. *   02Nov92 BAG Added GetIdentifier
  24. *   19Mar93 BAG Added _emPrompt1 from EditMaster file emsup.cb
  25. *
  26. ****************************************************************************/
  27. /*  Definitions     */
  28.  
  29. #include <cbrief.h>
  30.  
  31. /***************************************************************************/
  32. /*  Externals       */
  33.  
  34. string read_marked (void);
  35.  
  36. int getyorn (string msg);
  37.  
  38. int _chk_abort (~string);
  39.  
  40. string GetIdentifier (~string);
  41.  
  42. /***************************************************************************/
  43.  
  44. /****************************************************************************
  45. *
  46. *   read_marked                                         18Nov92
  47. *
  48. *   Return marked string
  49. *
  50. *   returns null string if more than one line is marked
  51. *   leaves marking if unsuccessful
  52. *
  53. *   If arg is non-zero, don't trim at all
  54. *
  55. *   Change History:
  56. *   Date    Who What
  57. *   ------- --- ---------------------
  58. *   09Mar90 BAG Killed allowing multiple lines
  59. *   19Aug92 BAG Added arg to suppress trimming leading, trailing blanks
  60. *   18Nov92 BAG Return null string if nothing at all is marked
  61. *
  62. ****************************************************************************/
  63.  
  64. string read_marked (~int donttrim)
  65.  
  66.  {  int start_line,
  67.         end_line,
  68.         start_col,
  69.         end_col,
  70.         mark_type;
  71.  
  72.     string line;
  73.  
  74.     mark_type = inq_marked (start_line, start_col, end_line, end_col);
  75.     if (mark_type == 0)
  76.         return ("");
  77.  
  78.     save_position ();
  79.  
  80.     if (start_line != end_line)
  81.         return ("");
  82.  
  83.     raise_anchor ();
  84.  
  85.     /*  Read the line into a string, and figure out how much */
  86.     /*  of it was marked.                                    */
  87.  
  88.     if (mark_type == LINE_MARK)
  89.        start_col = 1;
  90.  
  91.     move_abs (start_line, start_col);
  92.     line = read ();
  93.  
  94.     //@@@ if (! donttrim)
  95.     //@@@     line = trim (ltrim (line));
  96.  
  97.     /*  For line marks, we want the whole line after trimming */
  98.     /*  leading and trailing whitespace.  For other types of marks, */
  99.     /*  we have to try to return just the marked part of the line. */
  100.  
  101.     if (mark_type != LINE_MARK)
  102.      {  end_col -= start_col - 1;
  103.         line = substr (line, 1, end_col);
  104.      }
  105.  
  106.     if (! donttrim)
  107.         line = trim (ltrim (line));
  108.  
  109.     returns (line);
  110.  
  111.     restore_position ();
  112.  }
  113.  
  114. /****************************************************************************
  115. *
  116. *   getyorn                                             19Apr90
  117. *
  118. *   getyorn - Display prompt with " (Y/N)? " added and return YES or NO
  119. *
  120. *   Change History:
  121. *   Date    Who What
  122. *   19Apr90 BAG First draft
  123. *
  124. ****************************************************************************/
  125.  
  126. int getyorn (string msg)
  127.  
  128.  {  int key;
  129.  
  130.     message ("%s (Y/N)? ", msg);
  131.  
  132.     while (FOREVER)
  133.      {  key = read_char () % 256;
  134.  
  135.         if (key == 121 || key == 89)        // 'y' || 'Y'
  136.          {  key = YES;
  137.             break; 
  138.          }
  139.         else if (key == 110 || key == 78)   // 'n' || 'N'
  140.          {  key = NO;
  141.             break; 
  142.          }
  143.      }
  144.  
  145.     /*  Erase message  */
  146.     message ("");
  147.  
  148.     return (key);
  149.  }
  150.  
  151. /****************************************************************************
  152. *
  153. *   _chk_abort                                          19Mar93
  154. *
  155. *   Looks for any key hit, asks for confirm of abort
  156. *
  157. *   Returns TRUE iff caller is to abort processing
  158. *
  159. *   Change History:
  160. *   Date    Who What
  161. *   ------- --- -------------
  162. *   04May92 BAG First draft
  163. *   19Mar93 BAG Added optional message arg
  164. *
  165. ****************************************************************************/
  166.  
  167. int _chk_abort (~string)
  168.  
  169.  {  int ch;
  170.     string msg;
  171.  
  172.     if (! get_parm (0, msg))
  173.         msg = "Abort processing";
  174.  
  175.     if (inq_kbd_char ())
  176.      {  ch = read_char ();
  177.         return (getyorn (msg));
  178.      }
  179.     else 
  180.         return (FALSE);
  181.  }
  182.  
  183. /****************************************************************************
  184. *
  185. *   GetTempDir                                          04Jul92
  186. *
  187. *   Returns usable temporary directory, from drive letter through backslash
  188. *
  189. *   Change History:
  190. *   Date    Who What
  191. *   ------- --- -------------
  192. *   04Jul92 BAG First draft
  193. *
  194. ****************************************************************************/
  195.  
  196. string GetTempDir ()
  197.  
  198.  {  string tmpdir;
  199.  
  200.     tmpdir = trim (ltrim (inq_environment ("BTMP")));
  201.     if (tmpdir == "")
  202.         tmpdir = trim (ltrim (inq_environment ("TMP")));
  203.     if (tmpdir != "" && substr (tmpdir, strlen (tmpdir), 1) != "\\")
  204.         tmpdir += "\\";
  205.  
  206.     return (tmpdir);
  207.  }
  208.  
  209. /****************************************************************************
  210. *
  211. *   GetIdentifier                                       06Mar93
  212. *
  213. *   Arg is optional string of valid identifier characters
  214. *       (if omitted, use standard C chars)
  215. *
  216. *   Returns identifier found (or null string); leaves cursor at original
  217. *       position
  218. *
  219. *   @@@: Cf. _grabname in bhelp.cb for another way to do this
  220. *
  221. *   Change History:
  222. *   Date    Who What
  223. *   ------- --- -------------
  224. *   02Nov92 BAG First draft
  225. *
  226. ****************************************************************************/
  227.  
  228. string GetIdentifier (~string)
  229.  
  230.  {  string Ident, ValidChars = "a-zA-Z0-9_";
  231.     int gotword, length;
  232.  
  233.     get_parm (0, ValidChars);
  234.  
  235.     save_position ();
  236.  
  237.     /* Is cursor on [a-zA-Z0-9_]? */
  238.     if (search_string ("["+ValidChars+"]", read (1)))
  239.         gotword = 1;
  240.     else
  241.      {  /* not on a token; is there one to leftwards? */
  242.         search_back ("<|["+ValidChars+"]");
  243.         if (search_string ("["+ValidChars+"]", read (1)))
  244.             gotword = 1;
  245.         else
  246.             gotword = 0;
  247.      }
  248.  
  249.     if (gotword)
  250.      {  search_back ("<|[~"+ValidChars+"]");
  251.         if (search_string ("[~"+ValidChars+"]", read (1)))
  252.             next_char ();
  253.         Ident = read ();
  254.         length = search_string ("[~"+ValidChars+"]", Ident);
  255.         Ident = substr (Ident, 1, length-1);
  256.      }
  257.     else
  258.         Ident = "";
  259.  
  260.     restore_position ();
  261.  
  262.     return (Ident);
  263.  }
  264.  
  265. /****************************************************************************
  266. *
  267. *   _emPrompt1                                          19Mar93
  268. *
  269. *   Prompts for a single character response.  Only certain characters
  270. *   are considered valid, others are ignored.  ESC produces the old
  271. *   "Command cancelled." message.
  272. *
  273. *   Returns 0 if cancelled, otherwise the ASCII value of the char pressed
  274. *
  275. *   Change History:
  276. *   Date    Who What
  277. *   ------- --- -------------
  278. *   19Mar93 BAG First swiped from EditMaster emsup.cb
  279. *
  280. ****************************************************************************/
  281.  
  282. int _emPrompt1 (string prompt, string default, string valid_chars)
  283.  
  284.  {  string resp;
  285.  
  286.     for (;;)
  287.      {  if (get_parm (NULL, resp, prompt, 1, default) == 0 || resp == "")
  288.          {  //@@@ _emCan ();
  289.             return (0);
  290.          }
  291.  
  292.         if (search_string (resp, valid_chars))
  293.             return (atoi (resp, 0));
  294.      }
  295.  }
  296.  
  297. /************************* end of bag_util.cb file *************************/
  298.